﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <view.h>
#include<Game.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->resize(980,900);  // 设置窗口大小

    //显示初始化View界面
    view.mainlayout = new QVBoxLayout(ui->centralwidget); // 设置主布局
    readOptimalScore(); //读取最高分记录
    view.layout(game.getData(),game.getOptimalScore()); //初始化界面

    //连接信号与槽
    connect(view.voiceSlider, SIGNAL(valueChanged(int)), view.music, SLOT(setVolume(int)));  // 连接滑块与音量
    connect(view.restart, SIGNAL(clicked()), this, SLOT(restart())); //连接restart按钮的点击信号和槽函数
    connect(view.ai, SIGNAL(clicked()), this, SLOT(aiButton())); //连接ai按钮的点击信号和槽函数
    connect(view.tip, SIGNAL(clicked()), this, SLOT(tip())); //连接tip按钮的点击信号和槽函数

    //初始化变量
    flagAi = 0; //游戏开始时设置ai为关闭状态

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readOptimalScore() //从文件读取最高分记录
{
    std::ifstream infile("data.txt");
    std::string temp = "0"; //如果没有文件则置0
    getline(infile,temp);
    game.setOptimalScore(std::stoi(temp));
    infile.close();
}

void MainWindow::writeOptimalScore()    //将最高分写入文件
{
    std::ofstream outfile("data.txt");
    std::string temp = std::to_string(game.getOptimalScore());
    outfile << temp << std::endl;
    outfile.close();
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    int directionID = 0;
    switch (event->key())
    {
    case Qt::Key_W:
        directionID = 1;
        break;
    case Qt::Key_S:
        directionID = 2;
        break;
    case Qt::Key_A:
        directionID = 3;
        break;
    case Qt::Key_D:
        directionID = 4;
        break;
    default:
        break;
    }

    if(game.move(directionID))  //如果移动成功
    {
        int currentScore = game.getCurrentScore();
        int optimalScore = game.getOptimalScore();
        int comboNumber = game.getComboNumber();
        view.renew(game.getData(),currentScore);
        if(currentScore > optimalScore) //如果当前分大于最高分则重置最高分
        {
            game.setOptimalScore(currentScore);
            optimalScore = game.getOptimalScore();
            view.displayOptimalScore(optimalScore);
        }
        view.renewTip(0);   //重置tip
        if(game.passGame()) //通关
        {
            if(currentScore == optimalScore) writeOptimalScore();
            view.display(1, 0, comboNumber);
        }
        else if(game.gameOver())    //失败
        {
            if(currentScore == optimalScore) writeOptimalScore();
            view.display(0, 1, comboNumber);
        }
        else    //继续
        {
            view.display(0, 0, comboNumber);
        }
    }
}

void MainWindow::restart()
{
    if(game.getCurrentScore() == game.getOptimalScore()) writeOptimalScore();
    game.restart();
    view.renew(game.getData(),game.getCurrentScore());
    flagAi = 0; //游戏开始时设置ai为关闭状态
    view.ai->setStyleSheet("background:rgb(120, 120, 120)");    //设置ai按钮背景为灰色
    view.display(0, 0, game.getComboNumber());  //重置combo、gamePass和gameOver
    view.renewTip(0);   //重置tip
}

void MainWindow::tip()
{
    view.renewTip(ai.stepAi(game.getData()));
}

void MainWindow::aiButton()
{
    flagAi = !flagAi;
    if(flagAi) view.ai->setStyleSheet("background:rgb(255, 0, 0)"); //如果ai为开启状态，设置ai按钮背景为红色
    else view.ai->setStyleSheet("background:rgb(120, 120, 120)");   //如果ai为关闭状态，设置ai按钮背景为灰色
    /*
    std::thread t(threadAi);    //开启线程t
    t.detach(); //设置线程t为可分离
    */

    std::thread t(&MainWindow::threadAi,this);    //开启线程t
    t.detach(); //设置线程t为可分离

}

void MainWindow::threadAi()
{

    while(flagAi && !game.passGame() && !game.gameOver())
    {
        int stepAi = ai.stepAi(game.getData());
        view.renewTip(stepAi);
        game.move(stepAi);
        int currentScore = game.getCurrentScore();
        int optimalScore = game.getOptimalScore();
        int comboNumber = game.getComboNumber();
        view.renew(game.getData(),currentScore);
        if(currentScore > optimalScore) //如果当前分大于最高分则重置最高分
        {
            game.setOptimalScore(currentScore);
            optimalScore = game.getOptimalScore();
            view.displayOptimalScore(optimalScore);
        }
        if(game.passGame()) //通关
        {
            if(currentScore == optimalScore) writeOptimalScore();
            view.display(1, 0, comboNumber);
            break;
        }
        else if(game.gameOver())    //失败
        {
            if(currentScore == optimalScore) writeOptimalScore();
            view.display(0, 1, comboNumber);
            break;
        }
        else    //继续
        {
            view.display(0, 0, comboNumber);
        }
        view.renewTip(0);   //重置tip
    }

    /*
    //测试
    int num = 0;
    while(flagAi)
    {
    std::cout << num++ <<std::endl;
    _sleep(1000);
    }
    */
}
